package com.kehutong.work.order.manage.service;

import com.kehutong.work.order.manage.entity.WorkOrderManageClassify;
import com.kehutong.work.order.manage.entity.WorkOrderManageType;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;

import java.util.List;
import java.util.Objects;

@WebService("/work/order/manage/type/service/api")
public class WorkOrderTypeServiceAPI {

    @Inject
    private JdbcSession jdbcSession;
    @ReqMapping("/listPage")
    public Object listPage(JSONObject jsonObject) {
        insertType();
        return Result.success(jdbcSession.findPage(WorkOrderManageType.class)
                                         .like(jsonObject, "name")
                                         .eq("deleted", false)
                                         .order("enable", "desc")
                                         .order("sort", "asc")
                                         .page(jsonObject)
                                         .exe());
    }

    public Object list(JSONObject jsonObject) {
        insertType();
        return Result.success(jdbcSession.findArray(WorkOrderManageType.class)
                                         .eq(jsonObject, "name")
                                         .eq("deleted", false)
                                         .eq("enable", true)
                                         .order("sort", "asc")
                                         .exe());
    }

    private void insertType(){
        try {
            WorkOrderManageType tsType = Pool.get(WorkOrderManageType.class, "1001");
            if (Objects.isNull(tsType)) {
                tsType = Pool.newInstance(WorkOrderManageType.class, "1001");
                tsType.setName("投诉");
                tsType.setSort(1);
                tsType.setEnable(true);
                tsType.setDeleted(false);
                jdbcSession.insert(tsType);
            }

            WorkOrderManageType bxType = Pool.get(WorkOrderManageType.class, "1002");
            if (Objects.isNull(bxType)) {
                bxType = Pool.newInstance(WorkOrderManageType.class, "1002");
                bxType.setName("报修");
                bxType.setSort(2);
                bxType.setEnable(true);
                bxType.setDeleted(false);
                jdbcSession.insert(bxType);
            }
        } catch (Exception e){
            // continue
        }

    }

    public Object save(WorkOrderManageType type) {
        if (checkSystemSetting(type)) {
            return Result.error(0, "系统默认工单类型不允许更改");
        }
        jdbcSession.insert(type);

        return Result.success();
    }


    public Object update(WorkOrderManageType type) {
        if (checkSystemSetting(type)) {
            return Result.error(0, "系统默认工单类型不允许更改");
        }
        jdbcSession.updateById(type);

        List<WorkOrderManageClassify> classifyList = jdbcSession.findArray(WorkOrderManageClassify.class)
                                                                .eq("typeId", type.getId())
                                                                .exe();
        classifyList.forEach(e -> {
            e.setTypeName(type.getName());
            jdbcSession.updateById(e);
        });

        return Result.success();
    }

    private boolean checkSystemSetting(WorkOrderManageType type) {
        return "1001".equals(type.getId()) || "1002".equals(type.getId());
    }

    public Object delete(WorkOrderManageType type) {
        if (checkSystemSetting(type)) {
            return Result.error(0, "系统默认工单类型不允许更改");
        }
        type.setDeleted(true);
        jdbcSession.updateById(type);

        return Result.success();
    }

    public Object updateSort(WorkOrderManageType type, Integer sort) {
        type.setSort(sort);
        jdbcSession.updateById(type);

        return Result.success();
    }

}
